<template>
    <Dialog v-model="dialogVisible" :title="dialogTitle" width="500">
        <el-form ref="formRef" v-loading="formLoading" :model="formData" :rules="formRules" label-width="95px">
            <el-form-item label="专业名称" prop="name">
                <el-input v-model="formData.name" placeholder="请输入专业名称"/>
            </el-form-item>
            <el-form-item label="状态" prop="status">
                <el-radio-group v-model="formData.status">
                    <el-radio v-for="dict in getIntDictOptions(DICT_TYPE.COMMON_STATUS)" :key="dict.value" :label="dict.value">
                        {{ dict.label }}
                    </el-radio>
                </el-radio-group>
            </el-form-item>
            <el-form-item label="备注">
                <el-input v-model="formData.remark" placeholder="请输入内容" type="textarea" :rows="6"/>
            </el-form-item>
        </el-form>
        <template #footer>
            <el-button :disabled="formLoading" type="primary" @click="submitForm">确 定</el-button>
            <el-button @click="dialogVisible = false">取 消</el-button>
        </template>
    </Dialog>
</template>
<script lang="ts" setup>
import {CommonStatusEnum} from '@/utils/constants'
import {useMajorApi} from "@/api/yxt/major";
import {DICT_TYPE, getIntDictOptions} from "@/utils/dict";
import {useMessage} from "@/components/Notice";

defineOptions({name: 'YxtMajorForm'})

const {t} = useI18n();
const majorApi = useMajorApi();

const dialogVisible = ref(false)
const dialogTitle = ref('')
const formLoading = ref(false)
const formType = ref('')
const formData = ref({
    id: undefined,
    name: '',
    remark: '',
    status: CommonStatusEnum.ENABLE
})
const formRules = reactive({
    name: [{required: true, message: '专业名称不能为空', trigger: ['blur', 'change']}],
})
const formRef = ref()

const open = async (type: string, id?: number) => {
    dialogVisible.value = true
    dialogTitle.value = t('action.' + type)
    formType.value = type
    resetForm()
    if (id) {
        formLoading.value = true
        try {
            formData.value = await majorApi.getMajor(id)
        } finally {
            formLoading.value = false
        }
    }
}
defineExpose({open})

const emit = defineEmits(['success'])
const submitForm = () => {
    formRef.value?.validate(async (valid: any) => {
        if (valid) {
            formLoading.value = true
            try {
                const data = formData.value
                if (formType.value === 'create') {
                    await majorApi.createMajor(data);
                    useMessage(t('common.createSuccess'));
                } else {
                    await majorApi.updateMajor(data);
                    useMessage(t('common.updateSuccess'));
                }
                dialogVisible.value = false
                emit('success')
            } finally {
                formLoading.value = false
            }
        }
    });
}

const resetForm = () => {
    formData.value = {
        id: undefined,
        name: '',
        remark: '',
        status: CommonStatusEnum.ENABLE
    }
    formRef.value?.resetFields()
}
</script>
